package cn.wyu.dao;

import cn.wyu.pojo.User;
import cn.wyu.util.JDBCUtil;

import java.sql.ResultSet;

public class UserDaoImpl implements UserDao {
    @Override
    public User findByLoginName(String loginName) {

        //操作数据库
        String sql = "select * from ec_user where login_name = ?";
        //rs是数据库返回的结果
        ResultSet rs = JDBCUtil.executeQuery(sql, loginName);//集合
        //将查询到的数据封装到对象中
        try{
            //创建一个user对象，并将查询到的数据封装到这个对象中
            if(rs.next()){
                User user = new User();
                int id = rs.getInt("id");
                user.setId(id);
                String loginname = rs.getString("LOGIN_NAME");
                user.setLoginName(loginname);
                String password = rs.getString("PASSWORD");
                user.setPassword(password);
                String email = rs.getString("EMAIL");
                user.setEmail(email);
                String phone = rs.getString("PHONE");
                user.setPhone(phone);
                String name = rs.getString("NAME");
                user.setName(name);

                return user;

            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public void register(String loginName, String password) {
        String sql = "insert into ec_user(login_name,password) values(?,?);";
        JDBCUtil.executeUpdate(sql,loginName,password);
    }
}
